﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VirtualPress.Infrastructure.Entities.Enum;
using VirtualPress.Portal.Biz;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Portal.WebLogic;
using VirtualPress.Infrastructure.HitMonitor;

namespace VirtualPress.Portal.Web.Areas.Journals.Controllers
{
#if !DEBUG
        [ErrorHandle]
#endif
    [Compress]
    public class HomeController : Controller
    {
        //
        // GET: /Journals/Home/

        public ActionResult List(int id = 0, int page = 0)
        {
            ViewBag.Id = id;
            ViewBag.IndustryName = ((Profession)id).Mapping();
            var journals = iCache.Get(String.Format("JournalBiz.GetAllJournal_{0}_{1}_{2}OfItemListBox", "IndType=" + id, page, 12), () =>
            {
                return new ItemListBox<Periodical>(JournalBiz.GetAllJournal(c => c.IndType == id && c.Type == (int)JournalType.technicality && c.IsActive, page, 12))
                    .BuildPage(JournalBiz.GetAllJournalForPage(c => c.IndType == id && c.Type == (int)JournalType.technicality && c.IsActive), page, 12,
                    new PageParameter()
                    {
                        IsActive = false,
                        Param_page = "page",
                        Style = "home"
                    });
            },600);
            ViewBag.Journals = journals;
            return View();
        }

        public ActionResult Detail(int id)
        {
            ViewBag.Hit = HitMonitor.Instance(HitName.Journal, id).GetValue<JournalHitPack>(true, (int)JournalGetAction.visit);
            var journal = new Periodical().Find(id.ToString());
            if (journal == null)
            {
                return Redirect("/journals/home/list");
            }
            ViewBag.Journal = journal;
            ViewBag.NewJournalItem = iCache.Get(String.Format("JournalBiz.GetNewJournalItemDetail_{0}_{1}_{1}", id, 0, 10), () =>
            {
                return JournalBiz.GetNewJournalItemDetail(id, 0, 10);
            }, 600);

            ViewBag.JournalLeaguer = iCache.Get(String.Format("JournalBiz.GetAllLeaguer_{0}_{1}_{2}", id, 0, 100), () => { return JournalBiz.GetAllLeaguer(id, 0, 100); }, 600);

            ViewBag.DownloadFile = iCache.Get(
                String.Format("CommonBiz.GetAllDownloadFile_JournalFile_{0}_0_10", id), () =>
                {
                    return CommonBiz.GetAllDownloadFile(DownloadType.JournalFile, OrganizationType.Journal, id, 0, 10);
                }, 600);

            ViewBag.News = iCache.Get(
                String.Format("CommonBiz.GetAllNews_JournalNews_{0}_0_10",id), () =>
                {
                    return CommonBiz.GetAllNews(NewsType.JournalNews, id, OrganizationType.Journal, 0, 10);
                }, 600);
            return View();
        }

        public ActionResult News(int id,int journal)
        {
            ViewBag.DownloadFile = iCache.Get(
                String.Format("CommonBiz.GetAllDownloadFile_JournalFile_{0}_0_10", journal), () =>
                {
                    return CommonBiz.GetAllDownloadFile(DownloadType.JournalFile, OrganizationType.Journal, journal, 0, 10);
                }, 600);
            ViewBag.Hit = HitMonitor.Instance(HitName.Journal, journal).GetValue<JournalHitPack>(true, (int)JournalGetAction.visit);
            var periodical = new Periodical().Find(journal.ToString());
            ViewBag.Journal = periodical;
            var item = new News().Find(id.ToString());
            if (item == null)
            {
                return Redirect("/");
            }
            ViewBag.News = item;
            return View();
        }
    }
}
